// pages/user/addAddress/addAddress.js
var QQMapWX = require('../../../lib/qqmap-wx-jssdk.js')
var qqmapssdk = new QQMapWX({
  key: 'XEFBZ-BFQKG-QCXQM-IUH47-AZMI7-BHBIP'
})
import { HttpUtil, Target } from '../../../utils/http_util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressText: '点击选择',
    phone: '',
    param:{
      address: '',
      roomNumber: '',
      consumerId: 1,
      lat : 0,
      lng: 0,
      name: '',
      sex: 1,
      phone: '',
      type: 1
    },
    sexList:[
      { name: '先生', value: 1, checked: true},
      { name: '女士', value: 0},
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    var opt = options;
    console.log(opt);
    var address = options.address;
    //if (Object.prototype.toString.call(opt) != '[object Undefined]'){
    if(Object.keys(opt) != 0){
      that.setData({
        'param.address': opt.address,
        'param.lat': opt.latitude,
        'param.lng': opt.longitude,
        addressText: opt.address,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  chooseAddress:function(e){
    wx.navigateTo({
      url: "/pages/user/map/map",
    })
  },
  radioChange:function(e){
    this.setData({
      'param.sex' : e.detail.value
    });
  },
  saveAddress:function(e){
    var that = this;
    let address = that.data.param.address + that.data.param.roomNumber
    let param = that.data.param;
    param.address = address;
    console.log(param);
    HttpUtil.post(Target.self, "/customerAddress/address/add",param).then(res => {
      console.log(res);
      wx.navigateTo({
        url: "/pages/user/addressManage/addressManage",
      })
    })
  },
  inputSet(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    })
  },
})